package pkgEs

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/pkgEs"
	pkgEsReq "github.com/flipped-aurora/gin-vue-admin/server/model/pkgEs/request"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type InterviewApi struct {
}

var interviewService = service.ServiceGroupApp.PkgEsServiceGroup.InterviewService

// CreateInterview 创建interview表
// @Tags Interview
// @Summary 创建interview表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body pkgEs.Interview true "创建interview表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /interview/createInterview [post]
func (interviewApi *InterviewApi) CreateInterview(c *gin.Context) {
	var interview pkgEs.Interview
	err := c.ShouldBindJSON(&interview)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := interviewService.CreateInterview(&interview); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteInterview 删除interview表
// @Tags Interview
// @Summary 删除interview表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body pkgEs.Interview true "删除interview表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /interview/deleteInterview [delete]
func (interviewApi *InterviewApi) DeleteInterview(c *gin.Context) {
	var interview pkgEs.Interview
	err := c.ShouldBindJSON(&interview)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := interviewService.DeleteInterview(interview); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteInterviewByIds 批量删除interview表
// @Tags Interview
// @Summary 批量删除interview表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除interview表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /interview/deleteInterviewByIds [delete]
func (interviewApi *InterviewApi) DeleteInterviewByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := interviewService.DeleteInterviewByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateInterview 更新interview表
// @Tags Interview
// @Summary 更新interview表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body pkgEs.Interview true "更新interview表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /interview/updateInterview [put]
func (interviewApi *InterviewApi) UpdateInterview(c *gin.Context) {
	var interview pkgEs.Interview
	err := c.ShouldBindJSON(&interview)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := interviewService.UpdateInterview(interview); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindInterview 用id查询interview表
// @Tags Interview
// @Summary 用id查询interview表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query pkgEs.Interview true "用id查询interview表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /interview/findInterview [get]
func (interviewApi *InterviewApi) FindInterview(c *gin.Context) {
	var interview pkgEs.Interview
	err := c.ShouldBindQuery(&interview)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if reinterview, err := interviewService.GetInterview(interview.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"reinterview": reinterview}, c)
	}
}

// GetInterviewList 分页获取interview表列表
// @Tags Interview
// @Summary 分页获取interview表列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query pkgEsReq.InterviewSearch true "分页获取interview表列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /interview/getInterviewList [get]
func (interviewApi *InterviewApi) GetInterviewList(c *gin.Context) {
	var pageInfo pkgEsReq.InterviewSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := interviewService.GetInterviewInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}
